package com.gwcloud.leetcode.offers;

import com.gwcloud.leetcode.common.datastructure.TreeNode;

/** 
* @ClassName   : Leetcode0070 
* @author      : ganwei 
* @date        : 2020年5月31日 
* @Description : TODO 
*/
public class Leetcode0070 {
    /**
     * -解题思路
     */
	public TreeNode buildTree(int[] preorder, int[] inorder) {
		return buildTree(preorder, 0, preorder.length-1, inorder, 0, inorder.length-1);
    }
	
	/**
	 * -递归构建,递归主体
	 */
	public TreeNode buildTree(int[] preorder, int pstart, int pend, int[] inorder, int istart, int iend) {
		if(pstart > pend) {
			return null;
		}
		
		TreeNode head = new TreeNode(preorder[pstart]);
		
		int i;
		for(i = istart; i <= iend; i++) {
			if(inorder[i] == preorder[pstart]) {
				break;
			}
		}
		
		head.left = buildTree(preorder, pstart+1, pstart+i-istart, inorder, istart, i-1);
		head.right = buildTree(preorder, pstart+i-istart+1, pend, inorder, i+1, iend);
		return head;
		
	}
	
    /**
     * -主函数
     */
	public static void main(String[] args) {
		
	}
}
